import QtQuick 1.1

Rectangle {

    id: block
    property variant figure: null

    //NOTE: this is workaround for https://bugreports.qt-project.org/browse/QTBUG-21122
    property bool istransparent: false
    property int boardHeight: 100

    Component.onCompleted: {
        block.width = block.figure.blockWidth;
        block.height = block.figure.blockHeight;
        block.boardHeight = block.figure.boardHeight;
    }

    function collisionDetection(){
        if(istransparent){
            return;
        }

        var board = block.figure.parent;
        var stepahead = 1;

        var obj = parent.mapToItem(board, x, y);
        var pointx = obj.x;
        var pointy = obj.y;
        pointx += width / 2;
        pointy += height + stepahead;

        var foreign = board.childAt(pointx, pointy);
        if((pointy > boardHeight)||(foreign !== null && foreign.objectName === "frozenBlk")){
            block.figure.state = "stop";
        }
    }

    Connections{
        target: figure
        onCheckmove:{
            collisionDetection();
        }
    }

    MouseArea{
        id: mouse
        anchors.fill: parent
        onPressedChanged: {
            if(istransparent){
                block.figure.repeatFromBlock(mouse);
            }else{
                block.figure.rotateFigure();
            }
        }
    }
}
